package damngoodcoffee.smart.interpreters;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.xml.sax.helpers.AttributesImpl;

import atunit.AtUnit;
import atunit.Container;
import atunit.MockFramework;
import atunit.Stub;
import atunit.Unit;

import com.google.inject.Inject;
import com.thoughtworks.selenium.CommandProcessor;

import damngoodcoffee.smart.commands.GroupCommand;
import damngoodcoffee.smart.commands.SeleniumCommand;
import damngoodcoffee.smart.tags.EndTag;
import damngoodcoffee.smart.tags.StartTag;

@RunWith(AtUnit.class)
@Container(Container.Option.GUICE)
@MockFramework(MockFramework.Option.JMOCK)
public class CodeInterpreterTest {

    @Unit
    @Inject
    CodeInterpreter interpreter;

    @Inject
    @Stub
    CommandProcessor processor;

    @Test
    public void itShouldHandleCode() {
	AttributesImpl atts = new AttributesImpl();
	atts.addAttribute("", "class", "", "", "code");
	StartTag t = new StartTag("div", "", "", atts);
	assertTrue(interpreter.canInterpret(t));
    }

    @Test
    public void itShouldGenerateASeleniumCommand() throws InterpreterException {
	AttributesImpl atts = new AttributesImpl();
	atts.addAttribute("", "class", "", "", "code");
	StartTag t = new StartTag("div", "", "", atts);
	interpreter.interpret(t);
	t = new StartTag("table", "", "", new AttributesImpl());
	interpreter.interpret(t);
	t = new StartTag("tr", "", "", new AttributesImpl());
	interpreter.interpret(t);
	t = new StartTag("td", "", "", new AttributesImpl());
	interpreter.interpret(t);
	String command = "command";
	interpreter.interpret(command);
	EndTag et = new EndTag("td", "", "");
	interpreter.interpret(et);
	t = new StartTag("td", "", "", new AttributesImpl());
	interpreter.interpret(t);
	String target = "target";
	interpreter.interpret(target);
	et = new EndTag("td", "", "");
	interpreter.interpret(et);
	t = new StartTag("td", "", "", new AttributesImpl());
	interpreter.interpret(t);
	String value = "value";
	interpreter.interpret(value);
	et = new EndTag("td", "", "");
	interpreter.interpret(et);
	et = new EndTag("tr", "", "");
	interpreter.interpret(et);
	et = new EndTag("table", "", "");
	interpreter.interpret(et);
	et = new EndTag("code", "", "");
	interpreter.interpret(et);
	GroupCommand c = new GroupCommand();
	SeleniumCommand cmd = new SeleniumCommand(processor);
	cmd.setCommand(command);
	cmd.setTarget(target);
	cmd.setValue(value);
	c.add(cmd);

	assertEquals(c, interpreter.getCommand());
    }
}
